﻿using System;
using System.Collections.Generic;
using System.Text;

namespace csharpcore7_ch3.Chapt3_3ObjectClass
{
    class Chapt3_3_BoxingAndUnBoxing
    {
        static void _Main()
        {
            int x = 9;
            object obj = x;     //box the int

            int y = (int)obj;   //Unbox the int
            obj = 9;        //9 是整数类型
            //long x1 = (long)obj; //InvalidCastException

            obj = 3.5;     //3.5是double类型
            //(double)是拆箱操作，(int)是数值转换操作
            int x1 = (int)(double)obj; // x is now 3

            object[] a1 = new string[3];    //Legal

            //数组和泛型的变量只能支持引用转换，不能支持装箱转换
            //object[] a2 = new int[3]; //error

            int i = 3;
            object boxed = i;
            i = 5;
            Console.WriteLine(boxed);//3
        }
    }
}
